package com.eshop.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.eshop.bean.AdminsBean;

public class AdminsLoginFilter implements Filter{

	public void destroy() {
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;

		List<String> path = new ArrayList<String>();
		path.add("/adminOrder/login.jsp");
		path.add("/BLoginServlet");

		if(path.contains(req.getServletPath()))
		{
			chain.doFilter(request, response);
		}
		else
		{
			HttpSession session = req.getSession();
			List<AdminsBean> admins = (List<AdminsBean>)session.getAttribute("admins");
			if(admins ==null)
			{
				res.sendRedirect(req.getContextPath()+"/adminOrder/login.jsp");
			}
			else
			{
			chain.doFilter(request, response);
			}
		}	
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		
	}
}
